<%@page import="com.google.gdata.data.extensions.When"%>
<%@page import="com.google.gdata.data.calendar.CalendarEventEntry"%>
<%@page import="com.google.gdata.data.extensions.Where"%>
<%@page import="com.google.gdata.data.Person"%>
<%@page import="com.bitzer.google.Calendar"%>
<%@page import="com.google.gdata.data.extensions.Organization"%>
<%@page import="com.google.gdata.data.extensions.PostalAddress"%>
<%@page import="com.google.gdata.data.extensions.StructuredPostalAddress"%>
<%@page import="com.google.gdata.data.extensions.Email"%>
<%@page import="com.bitzer.google.Utils"%>
<%@page import="com.google.gdata.client.calendar.CalendarService"%>

<%@page import="java.io.IOException"%>
<%@page import="com.google.gdata.util.ServiceException"%>
<%@page import="com.google.gdata.data.calendar.CalendarFeed"%>
<%@page import="com.google.gdata.data.calendar.CalendarEntry"%>
<%@page import="java.util.List"%>
<%@page import="com.google.gdata.client.calendar.CalendarQuery"%>
<%@page import="com.google.gdata.data.calendar.CalendarEventFeed"%>
<%@page import="com.bitzer.google.Constants"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="bitzer-component" content="data"/>


        <title>Calendar</title>
    </head>
    <body>
        <%

                    String username = (String) session.getAttribute("username");
                    String password = (String) session.getAttribute("password");

                    if (username == null || username == ""
                            || password == null || password == "") {
                        response.sendRedirect(request.getContextPath()+"/login.jsp");
                    }

                    Calendar calendar = Calendar.instance();
                    calendar.authenticate(username, password);
                    String startRowStr = request.getParameter("startRow");
                    String numberOfRowsStr = request.getParameter("numberOfRows");
                    String lastModifiedOn = request.getParameter("lastModifiedOn");

                    int startRow = Utils.stringToInt(startRowStr, 1);
                    if (startRow == 0) {
                        startRow = 1;
                    }
                    int numberOfRows = Utils.stringToInt(numberOfRowsStr, 50000);
                    if (numberOfRows == 0) {
                        numberOfRows = 50000;
                    }

        %>

        <table data-bitzer-data="true"
               data-bitzer-title="Calendar"
               data-bitzer-desc="List of events"
               data-bitzer-component="Calendar"
               data-bitzer-record-count="<%=calendar.recordCount%>"
               data-bitzer-start-index="<%=startRow%>"
               data-bitzer-number-of-rows="<%=numberOfRows%>">
            <tr>
                <th data-bitzer-field="id" data-bitzer-key-order="1">id</th>

                <th data-bitzer-field="title">Title</th>
                <th data-bitzer-field="starttime">Start Time</th>
                <th data-bitzer-field="endtime">End Time</th>
                <th data-bitzer-field="author">Author</th>
                <th data-bitzer-field="location">Location</th>
                <th data-bitzer-field="last-modified-on">Last Modified</th>
            </tr>
            <%
                        try {

                            CalendarEventFeed feed = calendar.getFeed(lastModifiedOn, startRow, numberOfRows, request);
                            CalendarEventEntry entry = null;
                            List<Person> author = null;
                            List<Where> location = null;
                            List<When> when = null;
                            String locations = "";
                            int j = 0;

                            for (int i = 0; i < feed.getEntries().size(); i++) {
                                entry = (CalendarEventEntry) feed.getEntries().get(i);

                                out.print(Utils.dataRowElement("id", entry.getId()));
                                out.print(Utils.dataCell(entry.getId()));
                                out.print(Utils.dataCell(entry.getTitle().getPlainText()));

                                location = entry.getLocations();
                                author = entry.getAuthors();
                                
                                when = entry.getTimes();

                                if (!when.isEmpty()) {
                                    out.print(Utils.dataCell(Utils.formattedDateTime(when.get(0).getStartTime())));
                                    out.print(Utils.dataCell(Utils.formattedDateTime(when.get(0).getEndTime())));
                                }else{
                                    out.print(Utils.dataCell(""));
                                    out.print(Utils.dataCell(""));
                                }

                                if (!author.isEmpty()) {
                                    out.print(Utils.dataCell(author.get(0).getName()));
                                }
                                    
                                if (!location.isEmpty()) {
                                    for (j = 0; j < location.size(); j++) {
                                        locations += location.get(j).getValueString();
                                    }
                                }
                                out.print(Utils.dataCell(locations));
                                locations = "";
                                out.print(Utils.dataCell(Utils.formattedDateTime(entry.getUpdated())));

                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

            %>

        </table>

    </body>
</html>
